/**
 * 
 */
package registnet.courseDemand.extras;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * @author Dexter Ryan Carrasco
 */
public class UnderLoadedComputer {
	public static void main(final String args[]) {
		final File recFile = new File(
				"C:/Documents and Settings/Dexter Ryan Carrasco/Desktop/REC.csv");
		final File scaFile = new File(
				"C:/Documents and Settings/Dexter Ryan Carrasco/Desktop/SCA_03_STUD_NUM_OF_SUBJ.csv");
		final File snaFile = new File(
				"C:/Documents and Settings/Dexter Ryan Carrasco/Desktop/SNA_03_STUD_NUM_OF_SUBJ.csv");
		final File underFile = new File(
				"C:/Documents and Settings/Dexter Ryan Carrasco/Desktop/underloaded.csv");
		final Map<String, StudentCourses> students = new HashMap<String, StudentCourses>();

		try {
			// use buffering, reading one line at a time
			// FileReader always assumes default encoding is OK!
			final BufferedReader input = new BufferedReader(new FileReader(
					recFile));
			// use buffering

			try {
				String line = null; // not declared within while loop
				String tokens[];

				/*
				 * readLine is a bit quirky : it returns the content of a line
				 * MINUS the newline. it returns null only for the END of the
				 * stream. it returns an empty String if two newlines appear in
				 * a row.
				 */
				while ((line = input.readLine()) != null) {
					tokens = line.split(",");
					final StudentCourses student = new StudentCourses();
					student.setStudentNumber(tokens[0]);
					student.setNumRec(Integer.parseInt(tokens[1]));

					students.put(tokens[0], student);
				}
			} finally {
				input.close();

			}
		} catch (final IOException ex) {
			ex.printStackTrace();
		}

		try {
			// use buffering, reading one line at a time
			// FileReader always assumes default encoding is OK!
			final BufferedReader input = new BufferedReader(new FileReader(
					scaFile));
			// use buffering

			try {
				String line = null; // not declared within while loop
				String tokens[];

				/*
				 * readLine is a bit quirky : it returns the content of a line
				 * MINUS the newline. it returns null only for the END of the
				 * stream. it returns an empty String if two newlines appear in
				 * a row.
				 */
				while ((line = input.readLine()) != null) {
					tokens = line.split(",");
					final StudentCourses student = students.get(tokens[0]);
					student.setSCAresult(Integer.parseInt(tokens[1]));
				}
			} finally {
				input.close();

			}
		} catch (final IOException ex) {
			ex.printStackTrace();
		}

		try {
			// use buffering, reading one line at a time
			// FileReader always assumes default encoding is OK!
			final BufferedReader input = new BufferedReader(new FileReader(
					snaFile));
			// use buffering

			try {
				String line = null; // not declared within while loop
				String tokens[];

				/*
				 * readLine is a bit quirky : it returns the content of a line
				 * MINUS the newline. it returns null only for the END of the
				 * stream. it returns an empty String if two newlines appear in
				 * a row.
				 */
				while ((line = input.readLine()) != null) {
					tokens = line.split(",");
					final StudentCourses student = students.get(tokens[0]);
					student.setSNAresult(Integer.parseInt(tokens[1]));
				}
			} finally {
				input.close();

			}
		} catch (final IOException ex) {
			ex.printStackTrace();
		}

		try {
			// use buffering, reading one line at a time
			// FileReader always assumes default encoding is OK!
			final BufferedWriter output = new BufferedWriter(new FileWriter(
					underFile));
			// use buffering

			try {
				output
						.write("STUDENT NO.,RECOMMENDED,SCA,SCA Remarks,SNA, SNA Remarks");
				output.newLine();
				for (final Iterator<String> stIter = students.keySet()
						.iterator(); stIter.hasNext();) {
					final String studentNumber = stIter.next();
					final StudentCourses student = students.get(studentNumber);
					final StringBuffer sb = new StringBuffer();
					sb.append(studentNumber);
					sb.append(',');
					sb.append(student.getNumRec());
					sb.append(',');
					sb.append(student.getSCAresult());
					sb.append(',');
					sb.append(student.getSCAresult() == student.getNumRec());
					sb.append(',');
					sb.append(student.getSNAresult());
					sb.append(',');
					sb.append(student.getSNAresult() == student.getNumRec());

					output.write(sb.toString());
					output.newLine();
				}

			} finally {
				output.close();

			}
		} catch (final IOException ex) {
			ex.printStackTrace();
		}
	}
}
